#!/usr/bin/env python
# $Id: test2-35.py df54dec975f4 2012/09/09 20:22:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: df54dec975f4 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-09-09 20:22:40Z $"

import optparse
import os
import sys

from mercurial import commands, ui, hg
from mercurial.node import hex
from mercurial.error import Abort, RepoError

from venya.lib import exception
from venya.lib import cmdparser
from venya.lib import stdlib

basename = lambda directory: os.path.basename(directory)

class scmutil(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-r", "--repo", type = "str",
            help = "Repository URL", dest = "repo"),
         optparse.make_option("-d", "--directory", type = "str",
            help = "Cloned directory location", dest = "directory"),
         optparse.make_option("-m", "--message", type = "str",
            help = "Commit message", dest = "message"),
   ) + cmdparser.GetOpt.option_list

   def __init__(self, **kwargs):
      super(stdlib.my_class(self), self).__init__(**kwargs)
      self._url = None
      self._dir = None
      self._ui = ui.ui()
      self._files = []
      self._repo = None

   def _check(self):
      if self._url and self._dir:
         try:
            if not os.path.exists(self._dir):
               commands.clone(self._ui, self._url, self._dir)
         except RepoError as err:
            self.error(err)
         except Abort as err:
            self.error(err)

         self._repo = hg.repository(self._ui, self._dir)
         self._cont = self._repo.changectx("tip")
         print(dir(self._cont))

   def add(self):
      self._check()

   def status(self):
      self._check()

   def handle(self):
      (options, args) = self.parse()
      try:
         commands = args[1]
      except IndexError:
         self.error("no commands defined!")

      self._files = args[2:]

      if options.repo:
         self._url = options.repo
      else:
         self.error("repository is not defined!")

      if options.directory:
         self._dir = options.directory
      else:
         self.error("repository directory is not defined!")

      if commands.lower() == "add":
         self.add()
      elif commands.lower() == "commit":
         if options.message:
            self.commit(options.message)
         else:
            self.error("No commit message defined!")
      elif commands.lower() == "update":
         self.update()
      elif commands.lower() == "status":
         self.status()


if __name__ == "__main__":
   scm = None
   try:
      scm = scmutil(version = 1.0, args = sys.argv)
      scm.handle()
   except exception.GetOptError as err:
      print(err)
      sys.exit(1)

# repo = hg.repository(ui.ui(), "ssh://cvs.kashyapa.net//htdocs/mercurial")

